--- title: "L1-048 矩阵A乘以B" created: 2025-11-28 tags: - 算法 --- # L1-048 矩阵A乘以B ## 题目 [L1-048 矩阵A乘以B](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=994805082313310208&page=0) ![[image-5a84fb37.png]] ## 思路分析 ## 代码实现 ```cpp #include using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]= {-1,0,1,0},dy[4]= {0,1,0,-1}; const int inf = 0x3f3f3f3f; // A 有Ra行 Ca列 // B 有Rb行 Cb列 // 只有Ca==Rb时才能相乘 // 矩阵相乘方法: // 第一个矩阵第i行上的n个数 // 与第二个矩阵第j列上的n个数 // 对应相乘 // 后所得的n个乘积之和为第i行第j列位置上的数。 // 1 2 3 // 4 5 6 // 7 8 9 0 // -1 -2 -3 -4 // 5 6 7 8 // 1*7 + 2*(-1) + 3*5 = 20 // 1*8 + (-2)*2 + 3*6 = 22 // 4*7 + 5*(-1) + 6*5 = 53 // 20 22 …… // 53 int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int x1,y1,x2,y2; cin>>x1>>y1; int a1[x1][y1]; for(int i=0; i>a1[i][j]; } } cin>>x2>>y2; int a2[x2][y2]; for(int i=0; i>a2[i][j]; } } if(x2!=y1) { cout<<"Error: "<